package com.cloudptt.api.product.func.call.group.fsm.video.trans;

import android.content.Context;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.models.call.RtcpMsg;
import com.cloudptt.api.product.func.addr.IAddrOp;
import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.CallTimer;
import com.cloudptt.api.product.func.call.group.GroupCallTransListener;
import com.cloudptt.api.product.func.call.group.fsm.video.IGroupVideoMemberUpdate;
import com.cloudptt.api.product.func.call.group.fsm.video.IVideoSubState;
import com.cloudptt.api.product.func.call.group.fsm.video.GroupCallMemberInfo;
import com.cloudptt.api.product.func.call.group.fsm.video.recv.VideoRecvAgent;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.CallOperator;
import com.cloudptt.api.product.res.Resource;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.GroupMembInfo;

public class VideoTransAgent implements IVideoSubState
{
	public final static String Log_Key = "VideoTransAgent";
	private IVideoSubState currentState = null;
	private GroupCallTransListener listener = null;
	private Context ctx = null;
	private CallOperator operator = null;
	private IAddrOp addOp = null;
	private String curCallKey = null;
	private String callIdStr = CallAttr.DEF_CALL_ID_STR;
	//	private IRollback rollback = null;
	private CallTimer callTimer = null;

	//	private AddrInfo videoUserInfo = null;
//	private GroupCallMemberInfo info= new GroupCallMemberInfo();
	private IGroupVideoMemberUpdate memberUpdate = null;
	public VideoTransAgent(Context ctx, String curCallKey ,String callIdStr, CallOperator operator, IAddrOp addOp, CallTimer callTimer,IGroupVideoMemberUpdate memberUpdate)
	{
		Log.d(VideoTransAgent.Log_Key, "VideoTransAgent");
		this.ctx = ctx;
		this.curCallKey = curCallKey;
		this.callIdStr = callIdStr;
		this.operator = operator;
		this.addOp = addOp;
		this.callTimer = callTimer;
		this.memberUpdate = memberUpdate;
		this.currentState = new VideoTransNoPermissionState(this,true);
	}

	public void setListener(GroupCallTransListener listener)
	{
		Log.debug(this.getClass(), "setListener listener = " + listener);
		this.listener = listener;
	}

	public void goToState(int nextState){
		IVideoSubState videoState = null;
		Log.d(VideoTransAgent.Log_Key, "VideoTransAgent goToState:["+ CallConst.stateTag(this.currentState.getState()) +
				" to "+ CallConst.stateTag(nextState) + "]");

		switch (nextState) {
			case CallConst.VideoTransState_NoPermission:
				videoState = new VideoTransNoPermissionState(this,false);
				break;
			case CallConst.VideoTransState_PendingRequest:
				videoState = new VideoTransPendingRequestState(ctx,this);
				break;
			case CallConst.VideoTransState_Permission:
				videoState = new VideoTransPermissionState(this);
				break;
			case CallConst.VideoTransState_PendingRelease:
				videoState = new VideoTransPendingReleaseState(ctx,this);
				break;
			default:
				Log.e(VideoTransAgent.Log_Key, "Illegal state="+ nextState);
				return;//!!!
		}
		this.setCurrentState(videoState);
	}

	private void setCurrentState(IVideoSubState currentState)
	{
		Log.d(VideoTransAgent.Log_Key, "setCurrentState currentState= " + CallConst.stateTag(this.currentState.getState()));
		this.currentState.destory();
		this.currentState = currentState;

		if(listener != null )
		{
			listener.updateTransState(curCallKey,currentState.getState());
		}
		if(currentState != null
			&& currentState.getState() == CallConst.VideoTransState_NoPermission)
		{
			memberUpdate.clearGroupCallMemberRecvState();
		}
	}
	public void notifyTransUserInfo(String num,String jsonStr)
	{
		Log.e(VideoTransAgent.Log_Key, "notifyTransUserInfo num="+ num + " jsonStr = " + jsonStr + " listener = " + listener);
		if(num != null
				&& !num.equals(""))
		{
			Log.e(VideoTransAgent.Log_Key, "notifyTransUserInfo num="+ num + " 1" );
			AddrInfo videoUserInfo = addOp.getUserInfoByNum(num);
			GroupMembInfo memberInfo = videoUserInfo.toGroupCallMemberInfo("");
			GroupCallMemberInfo info= new GroupCallMemberInfo(memberInfo,jsonStr);
			info.updateTrans(true);
			memberUpdate.updateGroupCallMemberInfo(IGroupVideoMemberUpdate.Type_Trans,info);
//			if(listener != null) {
//				listener.notifyTransUserInfo(curCallKey, info);
//			}
		}
//		else
//		{
//			Log.e(VideoTransAgent.Log_Key, "notifyTransUserInfo num="+ num + " 2" );
//			info.update(null,"");
//			if(listener != null) {
//				listener.notifyTransUserInfo(curCallKey, info);
//			}
//		}
	}

	public void notifyTransEndUserInfo(String num,String jsonStr)
	{
		Log.e(VideoTransAgent.Log_Key, "notifyTransEndUserInfo num="+ num + " jsonStr = " + jsonStr + " listener = " + listener);
		if(num != null
				&& !num.equals(""))
		{
			Log.e(VideoTransAgent.Log_Key, "notifyTransEndUserInfo num="+ num + " 1" );
			AddrInfo videoUserInfo = addOp.getUserInfoByNum(num);
			GroupMembInfo memberInfo = videoUserInfo.toGroupCallMemberInfo("");
			GroupCallMemberInfo info= new GroupCallMemberInfo(memberInfo,jsonStr);
			info.updateTrans(false);
			memberUpdate.updateGroupCallMemberInfo(IGroupVideoMemberUpdate.Type_Trans,info);
//			if(listener != null) {
//				listener.notifyTransEndUserInfo(curCallKey, info);
//			}
		}
//		else
//		{
//			Log.e(VideoTransAgent.Log_Key, "notifyTransEndUserInfo num="+ num + " 2" );
//			info.update(null,"");
//			if(listener != null) {
//				listener.notifyTransEndUserInfo(curCallKey, info);
//			}
//		}
	}

	public void notifyMediaRecepEnd(String num)
	{
		Log.e(VideoRecvAgent.Log_Key, "notifyMediaRecepEnd num="+ num + " listener = " + listener);
		if(num != null
				&& !num.equals(""))
		{
			Log.e(VideoRecvAgent.Log_Key, "notifyMediaRecepEnd num="+ num + " 1" );
			AddrInfo videoUserInfo = addOp.getUserInfoByNum(num);
			GroupMembInfo memberInfo = videoUserInfo.toGroupCallMemberInfo("");
			GroupCallMemberInfo info= new GroupCallMemberInfo(memberInfo,"");
			info.updateRecv(false);
			memberUpdate.clearGroupCallMemberRecvState(info);
//			if(listener != null) {
//				listener.notifyRecvEndUserInfo(curCallKey, info);
//			}
		}
//		else
//		{
//			Log.e(VideoRecvAgent.Log_Key, "notifyTransUserInfo num="+ num + " 2" );
//			GroupCallMemberInfo info= new GroupCallMemberInfo();
//			info.update(null,"");
//			if(listener != null) {
//				listener.notifyRecvEndUserInfo(curCallKey, info);
//			}
//		}
	}
	//	public void notifyCurrentState()
//	{
////		this.currentState.destory();
////		this.currentState = currentState;
//		int state = currentState.getState();
//		if(listener != null)
//		{
//			String name = this.currentState.getSpeakName();
//			listener.updateVideoState(state, name);
//		}
//	}
	public int doUserAction(int btnAction)
	{
		return CauseCont.OperateErr_NoAgent;
	}
	public int doUserAction(int btnAction,String jsonStr)
	{
		Log.d( VideoTransAgent.Log_Key,
				CallConst.stateTag(this.currentState.getState()) + " doUserAction = " + CallConst.userActionTag( btnAction ) + " jsonStr = " + jsonStr);

		return this.currentState.doUserAction(btnAction,jsonStr);
	}

	public void updateTime(CallTimer timer) {
		this.callTimer.updateTime(timer);
	}


	public void receiveRtcpMsg(RtcpMsg msg)
	{
		Log.d(VideoTransAgent.Log_Key, CallConst.stateTag(this.currentState.getState()) +" receiveRtcpMsg msg = " + msg);
		this.currentState.receiveRtcpMsg(msg);
	}
	public int getState()
	{
		return this.currentState.getState();
	}

	@Override
	public void destory() {
		// TODO Auto-generated method stub
		this.currentState.destory();
	}

	protected int sendTrans(int type,String userId,long audioSsrc,long videoSsrc)
	{
		Log.d(VideoTransAgent.Log_Key, CallConst.stateTag(this.currentState.getState()) +" sendTrans type = " + type +  " userId = " + userId + " audioSsrc = " + audioSsrc + " videoSsrc = " + videoSsrc);
		Context ctx = this.getCtx();
		if(null == operator
				|| null == ctx)
		{
			return CauseCont.OperateErr_SIP;
		}
		int result = operator.sendTransMsg(callIdStr,type,userId,audioSsrc,videoSsrc);
		if( CauseCont.OperateCheck_Suc != result ) {
			result = CauseCont.OperateErr_SIP;
		}
		return result;
	}

//	protected void opAudioMedia(int opType)
//	{
//		if(currentState != null)
//		{
//			Log.d(VideoTransAgent.Log_Key, CallConst.stateTag(this.currentState.getState()) + " opAudioMedia opType = " + opType);
//		}
//		else
//		{
//			Log.d(VideoTransAgent.Log_Key, "opAudioMedia opType = " + opType);
//		}
//		Context ctx = this.getCtx();
//		if(null == operator
//				|| null == ctx)
//		{
//			String errStr =  Resource.getString("operator_null");//ctx.getString(R.string.operator_null);
//			CommDlg.showToast(ctx,errStr);
//			return;
//		}
//
//		int result = operator.opAudioMedia(callIdStr,opType);
//		if(result != CauseCont.OperateCheck_Suc)
//		{
//			String errStr = CauseCont.getOperateCheckErrStr(result,ctx);
//			CommDlg.showToast(ctx,errStr);
//			return;
//		}
//	}

	protected void opVideoLocalWinShow(boolean isShow)
	{
		if(currentState != null)
		{
			Log.d(VideoRecvAgent.Log_Key, CallConst.stateTag(this.currentState.getState()) +" opVideoLocalWinShow isShow = " + isShow);
		}
		else
		{
			Log.d(VideoRecvAgent.Log_Key, "opVideoLocalWinShow isShow = " + isShow);
		}
		Context ctx = this.getCtx();
		if(null == operator
				|| null == ctx)
		{
			String errStr =  Resource.getString("operator_null");//ctx.getString(R.string.operator_null);
			CommDlg.showToast(ctx,errStr);
			return;
		}

		int result = operator.opVideoWinShow(callIdStr,true,isShow);
		if(result != CauseCont.OperateCheck_Suc)
		{
			String errStr = CauseCont.getOperateCheckErrStr(result,ctx);
			CommDlg.showToast(ctx,errStr);
			return;
		}
	}

	protected void opVideoMedia(int opType)
	{
		if(currentState != null)
		{
			Log.d(VideoTransAgent.Log_Key, CallConst.stateTag(this.currentState.getState()) + " opVideoMedia opType = " + opType);
		}
		else
		{
			Log.d(VideoTransAgent.Log_Key, "opVideoMedia opType = " + opType);
		}
		Context ctx = this.getCtx();
		if(null == operator
				|| null == ctx)
		{
			String errStr =  Resource.getString("operator_null");//ctx.getString(R.string.operator_null);
			CommDlg.showToast(ctx,errStr);
			return;
		}

		int result = operator.opVideoMedia(callIdStr,opType);
		if(result != CauseCont.OperateCheck_Suc)
		{
			String errStr = CauseCont.getOperateCheckErrStr(result,ctx);
			CommDlg.showToast(ctx,errStr);
			return;
		}
	}


	public Context getCtx() {
		return ctx;
	}

	public CallOperator getOperator() {
		return operator;
	}

	public CallTimer getCallTimer() {
		return callTimer;
	}

	public void clearResource()
	{
		currentState.destory();
		currentState = null;
		listener = null;
		ctx = null;
		operator = null;
	}
}
